﻿using EquipSecs300;
using SecsEquip;
using SecsEquip300;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace EquipSecs300
{
    public class GEM_SubstrateTracking : SecsEquipEx
    {
        public string m_pViewName;
        private GEM_Variables Variables { get; set; }
        public GEM_SubstrateTracking(CSecsEquip300 m_pSecs, SecsEquipHelper secsEquipHelper)
        {
            m_pName = "Substrate Tracking";
            this.m_pSecs = m_pSecs;
            this.SecsEquipHelper = secsEquipHelper;
        }

        public override void UpdateData()
        {
            Variables = SecsEquipHelper.Variables;
        }

        // 获取晶圆ID
        string GetWaferID()
        {
            string waferID = string.Empty;
            return waferID;
        }

        /// <summary>
        /// Subst 处理开始
        /// </summary>
        /// <param name="nState"></param>
        public void ChangeWaferState(string pWaferID, SubstrateProcessingState nState)
        {
            //string pWaferID = GetWaferID();
            if (pWaferID != null &&
                pWaferID.Length > 0)
            {
                m_pSecs.SubstProcessingStateChange(pWaferID, nState);
            }
            else
            {
                LogSubstrateMsg($"Error: WaferID为空");
                MessageBox.Show("Error: aferID为空");
            }
        }

        // 操作

        private void SetPresentValue(WorkStation_KE workStation_KE, SePresentData sePresentData)
        {
            int nOffsetVID = (int)workStation_KE;
            Variables.SetValue(10410 + nOffsetVID, sePresentData.WAFERID);
            Variables.SetValue(10411 + nOffsetVID, sePresentData.WAFER_ST.ToString());
            Variables.SetValue(10412 + nOffsetVID, sePresentData.PORTNO.ToString());
            Variables.SetValue(10413 + nOffsetVID, sePresentData.SLOTNO.ToString());
            Variables.SetValue(10414 + nOffsetVID, sePresentData.WFRESULT.ToString());
            Variables.SetValue(10415 + nOffsetVID, sePresentData.WAFERID_Lot.ToString());
        }

        private bool Ev_WaferProcessing_B;
        public void Ev_BtnProcessing_R()
        {
            Ev_WaferProcessing_B = false;
        }
        /// <summary>
        ///  Subst 处理开始
        /// </summary>
        public void Ev_WaferProcessing(string waferID, SePresentData sePresentData)
        {
            if (Ev_WaferProcessing_B) { return; }
            SetPresentValue(WorkStation_KE.InspST, sePresentData);
            ChangeWaferState(waferID, SubstrateProcessingState.IN_PROCESS);
            Ev_WaferProcessing_B = false;
        }
        private bool Ev_WaferProcessed_B;
        public void Ev_WaferProcessed_R()
        {
            Ev_WaferProcessed_B = false;
        }
        /// <summary>
        /// Subst 处理完成
        /// </summary>
        public void Ev_WaferProcessed(string waferID, SePresentData sePresentData)
        {
            if (Ev_WaferProcessed_B) { return; }
            SetPresentValue(WorkStation_KE.InspST, sePresentData);
            ChangeWaferState(waferID, SubstrateProcessingState.PROCESSED);
            Ev_WaferProcessed_B = true;
        }

        private bool Ev_WaferLost_B;
        public void Ev_WaferLost_R()
        {
            Ev_WaferLost_B = false;
        }
        public void Ev_WaferLost(string waferID, SePresentData sePresentData)
        {
            if (Ev_WaferLost_B) { return; }
            SetPresentValue(WorkStation_KE.InspST, sePresentData);
            ChangeWaferState(waferID, SubstrateProcessingState.LOST);
            Ev_WaferLost_B = true;
        }

        void LogSubstrateMsg(string log)
        {
            LogMsg("SubstrateTracking>>>: " + log);
        }

    }
}
